﻿using FlexFlow.Base.Interface;
using FlexFlow.Model;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlexFlow.DataProvider.MongoDB
{
    /// <summary>
    /// 表示工作流信息提供者的MongoDb实现
    /// </summary>
    public class MongoDbWorkFlowInformationProvider : MongoDBPhysicalModelProvider<WorkFlowInformation>, IWorkFlowInformationProvider
    {
        /// <summary>
        /// 设置指定的信息项的值
        /// </summary>
        /// <param name="identity">
        /// 信息项的Id
        /// </param>
        /// <param name="value">
        /// 信息项的值
        /// </param>
        /// <remarks>
        /// 该操作要级联更新LastModifyTime
        /// </remarks>
        /// <returns>
        /// 一个值，表示更新是否成功
        /// </returns>
        public bool SetValue(Guid identity, string value)
        {
            var updef = BuildUpdateDef(m => m.Value, value);
            updef = BuildUpdateDef(m => m.LastModifyTime, DateTime.Now, updef);
            return Update(identity, updef);
        }
        /// <summary>
        /// 获取指定的信息项
        /// </summary>
        /// <param name="dictionary">
        /// 信息项的目录
        /// </param>
        /// <returns>
        /// 获取到的信息项
        /// </returns>
        public WorkFlowInformation Get(string dictionary, string key)
        {
            FilterDefinition<WorkFlowInformation> filter = (FilterDefinition<WorkFlowInformation>)(m => m.Directory == dictionary && m.Key == key);
            return this.GetCollection().FirstOrDefault(filter);
        }

        public override WorkFlowInformation Create()
        {
            return new WorkFlowInformation()
            {
                CreateTime = DateTime.Now
            };
        }
    }
}
